<?PHP
// vim: set expandtab tabstop=4 shiftwidth=4:
// +----------------------------------------------------------------------+
// | SAPID: XML Sapiens Engine Demonstrator                               |
// +----------------------------------------------------------------------+
// | Author:  Max Baryshnikov aka Mephius <mb@redgraphic.com>,	          |
// | Dmitry Sheiko <sheiko@cmsdevelopment.com>	                		  |
// | Copyright (c) 2004-2005 Max Baryshnikov                              |
// | http://sapid.sourceforge.net	                                      |
// +----------------------------------------------------------------------+
// | This source file is free software; you can redistribute it and/or    |
// | modify it under the terms of the GNU Lesser General Public           |
// | License as published by the Free Software Foundation; either         |
// | version 2.1 of the License, or (at your option) any later version.   |
// |                                                                      |
// | This source file is distributed in the hope that it will be useful,  |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU    |
// | Lesser General Public License for more details.                      |
// +----------------------------------------------------------------------+
// Release: 24.02.05 (dd/mm/yy)
// $Id: document_identification.inc.php,v 1.7 2005/10/20 11:12:43 sheiko Exp $

if (!defined("SAPID_STARTED")) die("Hacking attempt!");

$pointer="/";
if (is_array($env["argv"])) foreach ($env["argv"] as $arg) $pointer.=$arg . "/"; else $pointer="/";


if (!$env["page"] AND $_GET["paction"]!="seoanalizer" AND $_GET["paction"]!="convertor") {
	/*
	(
    [ID] => 0
    [TITLE] => Error 404
    [TEMPLATE] => error404.tpl
	)
	*/
	
	header ("HTTP/1.0 404 Not Found");
	$env["argv"] = array();
	$env["argv"][0] = "404error";
	$code="\$env[\"page\"]=\$tree->structure[\"root\"][\"404error\"];";
	@eval($code);	
	
	// Notification about 404 error
	if(!$env["page"])
		display_die("404error_page.tpl");
}

$env["page"]["VAR"]=$env["last_argv"]=$env["argv"][count($env["argv"])-1];
if(count($env["argv"])-2>0) $env["page"]["PARENT_VAR"]=$env["prev_last_argv"]=$env["argv"][count($env["argv"])-2];
$env["page"]["LEVEL"]=count($env["argv"]);
$env["page"]["OWNER"]=$tree->map[$env["page"]["ID"]]["OWNER"];
$env["page"]["GROUP"]=$tree->map[$env["page"]["ID"]]["GROUP"];
$env["page"]["PERMISSIONS"]=$tree->map[$env["page"]["ID"]]["PERMISSIONS"];

$pRead=false;
$pWrite=false;
$pCreate=false;
if($_SESSION["user"]["LOGIN"]==$env["page"]["OWNER"] OR $_SESSION["user"]["LOGIN"]==$admin_login){
	if($env["page"]["PERMISSIONS"]!=""){
		$pCurrent=(int)substr($env["page"]["PERMISSIONS"], 0, 1);
		if($pCurrent>=4) $env["page"]["pRead"]=true; else $env["page"]["pRead"]=false;
		if($pCurrent>=6) $env["page"]["pWrite"]=true; else $env["page"]["pWrite"]=false;
		if($pCurrent==7) $env["page"]["pCreate"]=true; else $env["page"]["pCreate"]=false;
	}
}else{
	if($env["page"]["PERMISSIONS"]!=""){
		if($_SESSION["user"]["GROUP"]==$env["page"]["GROUP"]){
			$pCurrent=(int)substr($env["page"]["PERMISSIONS"], 1, 1);
		}else{
			$pCurrent=(int)substr($env["page"]["PERMISSIONS"], 2, 1);
		}
		if($pCurrent>=4) $env["page"]["pRead"]=true; else $env["page"]["pRead"]=false;
		if($pCurrent>=6) $env["page"]["pWrite"]=true; else $env["page"]["pWrite"]=false;
		if($pCurrent==7) $env["page"]["pCreate"]=true; else $env["page"]["pCreate"]=false;
	}
}

if(!$env["page"]["pRead"] AND $_GET["paction"]!="seoanalizer" AND $_GET["paction"]!="convertor") {
	header("Location: " . $env["http_path"] . "404/");
	exit;
}
if($default_language=="ru") $img_language = "ru"; else $img_language = "en";

if($env["virtual_path_mask"]) {
	$mask_arr = explode("/", preg_replace("/\/$/", "", $env["virtual_path_mask"]));
	if(($env["page"]["MASK"] == "#+" OR $env["page"]["MASK"] == "#") AND count($mask_arr)==4) {
		$env["argv_virtual.year"] = $mask_arr[0];
		$env["argv_virtual.month"] = $mask_arr[1];
		$env["argv_virtual.day"] = $mask_arr[2];
		$env["argv_virtual.id"] = $mask_arr[3];
	} elseif($env["page"]["MASK"] == "#+" AND count($mask_arr)==1) $env["argv_virtual.id"] = $mask_arr[0];
	if($env["page"]["MASK"] == "####" AND $env["argv_virtual.year"] = $mask_arr[0]);
} 


$env["document.id"] = $env["page"]["ID"];
$env["document.variable"] = $env["page"]["VAR"];
$env["document.title"] = $env["page"]["TITLE"];
$env["document.template"] = $env["page"]["TEMPLATE"];
$env["document.level"] = $env["page"]["LEVEL"];
$env["document.datetime"] = $env["page"]["DATE_CREATE"];
$env["document.children"] = $env["page"]["CHILDREN"];

if($env["virtual_path_mask"])
	$env["base.url"] = substr($env["document.url"], 0, strlen($env["document.url"]) - strlen($env["virtual_path_mask"]));
else $env["base.url"] = $env["document.url"];


$env["argv_nav.rowselect"] = ($env["argv_nav.1"]?$env["argv_nav.1"]:"none");

if($env["argv_nav"][0]) {
	$last_nav_argv = $env["argv_nav"][count($env["argv_nav"])-1];
	if( preg_match("/^tpl-/is", $last_nav_argv) ) { 
		$env["page"]["TEMPLATE"] = preg_replace("/^tpl-/is", "", $last_nav_argv).".tpl";
	}
	if( preg_match("/^ic_/is", $env["argv_nav"][0]) ) { 
		$env["page"]["MASK"] = " ";
	}
}


?>